home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 267_01 / as6eval.c < prev    next >
C/C++ Source or Header  |  1989-01-11  |  11KB  |  465 lines

  1. /*
  2.     HEADER:        CUG267;
  3.     TITLE:        S6 Cross-Assembler (Portable);
  4.     FILENAME:    AS6EVAL.C;
  5.     VERSION:    0.2;
  6.     DATE:        08/27/1988;
  7.     SEE-ALSO:    AS6.H;
  8.     AUTHORS:    William C. Colley III;
  9. */
  10.  
  11. /*
  12.                S6 Cross-Assembler in Portable C
  13.  
  14.         Copyright (c) 1986, 1987 William C. Colley, III
  15.  
  16. Revision History:
  17.  
  18. Ver    Date        Description
  19.  
  20. 0.0    NOV 1987    Derived from my 6502 cross-assembler.  WCC3.
  21.  
  22. 0.1    JUNE 1988    Fixed glitches in the relative jumps and in the bit
  23.             operations to make the assembler conform to the
  24.             machine rather than to the machine's documentation.
  25.             WCC3.
  26.  
  27. 0.2    AUG 1988    Fixed a bug in the command line parser that puts it
  28.             into a VERY long loop if the user types a command line
  29.             like "AS6 FILE.ASM -L".  WCC3 per Alex Cameron.
  30.  
  31. This file contains the assembler's expression evaluator and lexical analyzer.
  32. The lexical analyzer chops the input character stream up into discrete tokens
  33. that are processed by the expression analyzer and the line assembler.  The
  34. expression analyzer processes the token stream into unsigned results of
  35. arithmetic expressions.
  36. */
  37.  
  38. /*  Get global goodies:  */
  39.  
  40. #include "as6.h"
  41.  
  42. /*  Get access to global mailboxes defined in AS6.C:            */
  43.  
  44. extern char line[];
  45. extern int filesp, forwd, pass;
  46. extern unsigned pc;
  47. extern FILE *filestk[], *source;
  48. extern TOKEN token;
  49.  
  50. /*  Machine opcode argument field parsing routine.  The token stream    */
  51. /*  from the lexical analyzer is processed to extract addresses and    */
  52. /*  addressing mode information.  The resulting value is passed back    */
  53. /*  through the same token buffer that the lexical analyzer uses.  In    */
  54. /*  addition, the addressing mode information is returned as the    */
  55. /*  function return value.                        */
  56.  
  57. static int bad;
  58.  
  59. unsigned get_arg(num)
  60. int num;
  61. {
  62.     SCRATCH int c;
  63.     SCRATCH unsigned a, u;
  64.     TOKEN *lex();
  65.     int popc();
  66.     unsigned eval(), expr();
  67.     void exp_error(), pushc(), trash(), unlex();
  68.  
  69.     a = NULL;  u = 0;  bad = FALSE;
  70.     switch (lex() -> attr & TYPE) {
  71.     case EOL:
  72.     case SEP:   exp_error('S');  break;
  73.  
  74.     case OPR:   if (token.valu == '(') {
  75.             u = eval(LPREN);  trash();  pushc(c = popc());
  76.             if (c == ',' || c == '\n') {
  77.                 if (u < 0x80 || u > 0x81) exp_error('V');
  78.                 else if (!forwd) a = IS_IND;
  79.                 lex();  break;
  80.             }
  81.             token.attr = VAL;  token.valu = u;
  82.             }
  83.  
  84.     case VAL:
  85.     case STR:   unlex();  u = eval(START);
  86.             if (!forwd) switch (u) {
  87.             case 0xff:  a = IS_A;  break;
  88.  
  89.             case 0x80:
  90.             case 0x81:
  91.             case 0x82:
  92.             case 0x83:  a = IS_XYVW;  break;
  93.  
  94.             default:    break;
  95.             }
  96.             break;
  97.     }
  98.     if (token.attr != (num ? EOL : SEP)) exp_error('S');
  99.     token.valu = bad ? 0 : u;  return token.attr = a;
  100. }
  101.  
  102. /*  Expression analysis routine.  The token stream from the lexical    */
  103. /*  analyzer is processed as an arithmetic expression and reduced to an    */
  104. /*  unsigned value.  If an error occurs during the evaluation, the    */
  105. /*  global flag    forwd is set to indicate to the line assembler that it    */
  106. /*  should not base certain decisions on the result of the evaluation.    */
  107.  
  108. unsigned expr()
  109. {
  110.     SCRATCH unsigned u;
  111.     unsigned eval();
  112.  
  113.     bad = FALSE;
  114.     u = eval(START);
  115.     return bad ? 0 : u;
  116. }
  117.  
  118. static unsigned eval(pre)
  119. unsigned pre;
  120. {
  121.    register unsigned op, u, v;
  122.    TOKEN *lex();
  123.    void exp_error(), unlex();
  124.  
  125.    for (;;) {
  126.       u = op = lex() -> valu;
  127.       switch (token.attr & TYPE) {
  128.      case SEP:   if (pre != START) unlex();
  129.      case EOL:   exp_error('E');  return;
  130.  
  131.      case OPR:   if (!(token.attr & UNARY)) { exp_error('E');  break; }
  132.              u = eval((op == '+' || op == '-') ?
  133.                (unsigned) UOP1 : token.attr & PREC);
  134.              switch (op) {
  135.             case '-':   u = word(-u);  break;
  136.  
  137.             case NOT:   u ^= 0xffff;  break;
  138.  
  139.             case HIGH:  u = high(u);  break;
  140.  
  141.             case LOW:   u = low(u);  break;
  142.              }
  143.  
  144.      case VAL:    
  145.      case STR:   for (;;) {
  146.             op = lex() -> valu;
  147.             switch (token.attr & TYPE) {
  148.                case SEP:   if (pre != START) unlex();
  149.                case EOL:   if (pre == LPREN) exp_error('(');
  150.                        return u;
  151.  
  152.                case STR:
  153.                case VAL:   exp_error('E');  break;
  154.  
  155.                case OPR:   if (!(token.attr & BINARY)) {
  156.                       exp_error('E');  break;
  157.                        }
  158.                        if ((token.attr & PREC) >= pre) {
  159.                       unlex();  return u;
  160.                        }
  161.                        if (op != ')')
  162.                       v = eval(token.attr & PREC);
  163.                        switch (op) {
  164.                       case '+':   u += v;  break;
  165.  
  166.                       case '-':   u -= v;  break;
  167.  
  168.                       case '*':   u *= v;  break;
  169.  
  170.                       case '/':   u /= v;  break;
  171.  
  172.                       case MOD:   u %= v;  break;
  173.  
  174.                       case AND:   u &= v;  break;
  175.  
  176.                       case OR:    u |= v;  break;
  177.  
  178.                       case XOR:   u ^= v;  break;
  179.  
  180.                       case '<':   u = u < v;  break;
  181.  
  182.                       case LE:    u = u <= v;  break;
  183.  
  184.                       case '=':   u = u == v;  break;
  185.  
  186.                       case GE:    u = u >= v;  break;
  187.  
  188.                       case '>':   u = u > v;  break;
  189.  
  190.                       case NE:    u = u != v;  break;
  191.  
  192.                       case SHL:   if (v > 15)
  193.                              exp_error('E');
  194.                               else u <<= v;
  195.                               break;
  196.  
  197.                       case SHR:   if (v > 15)
  198.                              exp_error('E');
  199.                               else u >>= v;
  200.                               break;
  201.  
  202.                       case ')':   if (pre == LPREN)
  203.                              return u;
  204.                               exp_error('(');
  205.                               break;
  206.                        }
  207.                        clamp(u);
  208.                        break;
  209.             }
  210.              }
  211.              break;
  212.       }
  213.    }
  214. }
  215.  
  216. static void exp_error(c)
  217. char c;
  218. {
  219.     forwd = bad = TRUE;  error(c);
  220. }
  221.  
  222. /*  Lexical analyzer.  The source input character stream is chopped up    */
  223. /*  into its component parts and the pieces are evaluated.  Symbols are    */
  224. /*  looked up, operators are looked up, etc.  Everything gets reduced    */
  225. /*  to an attribute word, a numeric value, and (possibly) a string    */
  226. /*  value.                                */
  227.  
  228. static int oldt = FALSE;
  229. static int quote = FALSE;
  230.  
  231. TOKEN *lex()
  232. {
  233.     SCRATCH char c, *p;
  234.     SCRATCH unsigned b;
  235.     SCRATCH OPCODE *o;
  236.     SCRATCH SYMBOL *s;
  237.     OPCODE *find_operator();
  238.     SYMBOL *find_symbol();
  239.     void exp_error(), make_number(), pops(), pushc(), trash();
  240.  
  241.     if (oldt) { oldt = FALSE;  return &token; }
  242.     trash();
  243.     if (isalph(c = popc())) {
  244.     pushc(c);  pops(token.sval);
  245.     if (o = find_operator(token.sval)) {
  246.         token.attr = o -> attr;  token.valu = o -> valu;
  247.     }
  248.     else {
  249.         token.attr = VAL;
  250.         if (!strcmp(token.sval,"$")) token.valu = pc;
  251.         else if (s = find_symbol(token.sval)) {
  252.         token.valu = s -> valu;
  253.         if (pass == 2 && s -> attr & FORWD) forwd = TRUE;
  254.         }
  255.         else { token.valu = 0;  exp_error('U'); }
  256.     }
  257.     }
  258.     else if (isnum(c)) {
  259.     pushc(c);  pops(token.sval);
  260.     for (p = token.sval; p[1]; ++p);
  261.     switch (toupper(*p)) {
  262.         case 'B':    b = 2;  break;
  263.  
  264.         case 'O':
  265.         case 'Q':    b = 8;  break;
  266.  
  267.         default:    ++p;
  268.         case 'D':    b = 10;  break;
  269.  
  270.         case 'H':    b = 16;  break;
  271.     }
  272.     *p = '\0';  make_number(b);
  273.     }
  274.     else switch (c) {
  275.     case '(':   token.attr = UNARY + LPREN + OPR;
  276.             goto opr1;
  277.  
  278.     case ')':   token.attr = BINARY + RPREN + OPR;
  279.             goto opr1;
  280.  
  281.     case '+':   token.attr = BINARY + UNARY + ADDIT + OPR;
  282.             goto opr1;
  283.  
  284.     case '-':   token.attr = BINARY + UNARY + ADDIT + OPR;
  285.             goto opr1;
  286.  
  287.     case '*':   token.attr = BINARY + UNARY + MULT + OPR;
  288.             goto opr1;
  289.  
  290.     case '/':   token.attr = BINARY + MULT + OPR;
  291. opr1:            token.valu = c;
  292.             break;
  293.  
  294.     case '<':   token.valu = c;
  295.             if ((c = popc()) == '=') token.valu = LE;
  296.             else if (c == '>') token.valu = NE;
  297.             else pushc(c);
  298.             goto opr2;
  299.  
  300.     case '=':   token.valu = c;
  301.             if ((c = popc()) == '<') token.valu = LE;
  302.             else if (c == '>') token.valu = GE;
  303.             else pushc(c);
  304.             goto opr2;
  305.  
  306.     case '>':   token.valu = c;
  307.             if ((c = popc()) == '<') token.valu = NE;
  308.             else if (c == '=') token.valu = GE;
  309.             else pushc(c);
  310. opr2:            token.attr = BINARY + RELAT + OPR;
  311.             break;
  312.  
  313.     case '\'':
  314.     case '"':   quote = TRUE;  token.attr = STR;
  315.             for (p = token.sval; (*p = popc()) != c; ++p)
  316.             if (*p == '\n') { exp_error('"');  break; }
  317.             *p = '\0';  quote = FALSE;
  318.             if ((token.valu = t